package com.kevin.fileproviderclient;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;

import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {
	ImageView ivImage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ivImage = (ImageView) findViewById(R.id.image);
	}

	public void onShowImage(View view) {
		Intent intent = new Intent();
		intent.setAction("android.intent.action.PICK");
		intent.addCategory("android.intent.category.OPENABLE");
		intent.setType("image/*");
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (0 == requestCode) {
			if (RESULT_OK == resultCode) {
				Uri uriFileName = data.getData();
				try {
					ParcelFileDescriptor sDescriptor = getContentResolver().openFileDescriptor(uriFileName, "r");
					FileDescriptor sFileDescriptor = sDescriptor.getFileDescriptor();
					Bitmap bm = BitmapFactory.decodeFileDescriptor(sFileDescriptor);
					ivImage.setImageBitmap(bm);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
